/**
* @description: 991. 坏了的计算器
* @author hewei
* @date 2022/10/4 15:42
* @version 1.0
*/

public class BrokenCalc {

    public int brokenCalc1(int startValue, int target) {
        int count = 0;
        while (startValue < target) {
            ++count;
            startValue *= 2;
        }
        if (startValue == target) return count;
        int delta = startValue - target;
        int index = count;
        while (delta > 0) {
            int pow = (int) Math.pow(2, index);
            count += delta / pow;
            delta %= pow;
            --index;
        }
        return count;
    }

    public int brokenCalc(int startValue, int target) {
        int ans = 0;
        while (target > startValue) {
            if (target % 2 == 0) target /= 2;
            else target += 1;
            ++ans;
        }
        return ans + startValue - target;
    }
}